package ru.incoremedia.gwttest.core.client.component;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import ru.incoremedia.gwttest.application.client.i18n.I18N;


public class ExtendedPopupPanel extends GlassPanel {

    private PopupPanel popupPanel;

    private HorizontalPanel menuPanel;

    private HorizontalPanel titlePanel;

    private HorizontalPanel controlPanel;

    private VerticalPanel windowPanel;

    private VerticalPanel contentPanel;

    public ExtendedPopupPanel(String title, String width, String height) {
        popupPanel = new PopupPanel(false, true);

        windowPanel = new VerticalPanel();
        windowPanel.setWidth(width);
        windowPanel.setHeight(height);
        windowPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
        popupPanel.add(windowPanel);

        contentPanel = new VerticalPanel();
        contentPanel.setWidth("100%");
        windowPanel.add(contentPanel);

        menuPanel = new HorizontalPanel();
        menuPanel.setWidth("100%");
        menuPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        menuPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        contentPanel.add(menuPanel);

        titlePanel = new HorizontalPanel();
        titlePanel.setWidth("100%");
        titlePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        titlePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        menuPanel.add(titlePanel);

        controlPanel = new HorizontalPanel();
        controlPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        controlPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        menuPanel.add(controlPanel);

        Hyperlink closeLink = new Hyperlink(I18N.constants.close(), "");
        closeLink.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                hide();
            }
        });

        controlPanel.add(closeLink);

        Label titleLabel = new Label(title);
        titleLabel.addStyleName("smallLabelWhite");
        titlePanel.add(titleLabel);
    }

    @Override
    public void onResize(ResizeEvent event) {
        if(popupPanel.isShowing()) {
            super.onResize(event);
            popupPanel.center();
        }
    }

    @Override
    public void show() {
        Window.enableScrolling(false);
        resize();
        super.show();
        popupPanel.center();
    }

    @Override
    public void hide() {
        super.hide();
        popupPanel.hide();
        Window.enableScrolling(true);
    }

    public VerticalPanel getWindowPanel() {
        return windowPanel;
    }

    public HorizontalPanel getMenuPanel() {
        return menuPanel;
    }

    public PopupPanel getPopupPanel() {
        return popupPanel;
    }

    public HorizontalPanel getTitlePanel() {
        return titlePanel;
    }

    public VerticalPanel getContentPanel() {
        return contentPanel;
    }
}